package copyengine.ui.text
{
	import copyengine.utils.debug.CELog;
	
	import flash.utils.Dictionary;

	public final class CETextManger
	{
		private static const REPLACE_NEWLINE:String = "#n#";
		private static var _instance:CETextManger;

		public static function get instance():CETextManger
		{
			if (_instance == null)
			{
				_instance = new CETextManger();
			}
			return _instance;
		}

		private var textDic:Dictionary;

		public function CETextManger()
		{
		}

		public function initialize(_languageXml:XML):void
		{
			textDic = new Dictionary();
			for each (var textNode:XML in _languageXml.data)
			{
				var key:String = textNode.@id;
				var value:String = textNode.@value;
				textDic[key] = value;
			}
		}

		public function getText(_textID:String):String
		{
			var word:String = textDic[_textID];
			if (!word)
			{
				CELog.err('i18n text not found for \'' + _textID + '\'');
				return "";
			}
			return convertKeyWord(word, REPLACE_NEWLINE, "\n");
		}

		public function convertKeyWord(word:String, key:String, convertTo:String):String
		{
			var myPattern:RegExp = new RegExp(key, "g");
			return word.replace(myPattern, convertTo);
		}

	}
}
